package com.dingding.service.account.core.tailwind.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;

import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.beans.Depositdetail;
import com.dingding.common.core.beans.PassangerDepositAccount;
import com.dingding.common.core.beans.UserLogin;
import com.dingding.common.core.beans.Useraccount;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.facade.account.common.dto.tailwind.GetTailwindDepositAccountDTO;
import com.dingding.facade.account.common.dto.tailwind.ReqAddDepositAccount;
import com.dingding.facade.account.common.dto.tailwind.ReqDeleteDepositAccount;
import com.dingding.facade.account.common.dto.tailwind.ReqGetDepositAccount;
import com.dingding.facade.account.common.dto.tailwind.ReqUAStatus;
import com.dingding.facade.account.common.dto.tailwind.ReqWithdrawals;
import com.dingding.facade.account.common.dto.tailwind.ResGetDepositAccount;
import com.dingding.facade.account.common.dto.tailwind.ResUAStatus;
import com.dingding.facade.account.common.dto.tailwind.TailwindWithdrawalsDTO;
import com.dingding.facade.user.facade.tailwind.ITailwindUserInfoFacade;
import com.dingding.service.account.core.tailwind.dao.IDepositAccountDao;
import com.dingding.service.account.core.tailwind.dao.ITailwindUserAccountDao;
import com.dingding.service.account.core.tailwind.service.IDepositAccountService;

/**
 * 
 * @ClassName: DepositAccountServiceImpl 
 * @Description: 提现账户业务逻辑实现类
 * @author YHQ
 * @date 2016年12月21日 下午3:55:10
 * @since V2.6.3
 * 
 */
@Component("depositAccountService")
public class DepositAccountServiceImpl implements IDepositAccountService{

	@Resource(name = "depositAccountDao")
	private IDepositAccountDao depositAccountDao;
	
	@Resource(name = "tailwindUserInfoFacade_tailwind")
	private ITailwindUserInfoFacade tailwindUserInfoFacade;
	
	@Resource(name = "tailwindUserAccountDao")
	private ITailwindUserAccountDao tailwindUserAccountDao;
	
	/**
	 * 
	 * @Title: getUserAuthenticationStatus 
	 * @Description: 获取顺风车用户提现认证状态 
	 * @param reqUAStatus 客户端请求参数
	 * @author YHQ 
	 * @since V2.5.8
	 * @return ResponseEntity<Object> 获取顺风车用户提现认证状态响应Bean
	 * 
	 */
	@Override
	public ResponseEntity<Object> getUserAuthenticationStatus(ReqUAStatus reqUAStatus) {
		
		ResponseEntity<Object> responseEntity = new ResponseEntity<Object>();
		
		// 客户端请求Bean是否为空
		if(reqUAStatus == null || !ToolUtil.validateParam(reqUAStatus)){
			
			responseEntity.setCode(HttpStatus.SC_OK);
			responseEntity.setErrorMessage("客户端请求数据为空");
			responseEntity.setPromptMessage("请求请求参数异常");
			
			return responseEntity;
		}
		
		// 获取顺风车用户提现认证状态 
		ResUAStatus resUAStatus = depositAccountDao.getUserAuthenticationStatus(reqUAStatus.getUserId());
		
		responseEntity.setCode(HttpStatus.SC_OK);
		responseEntity.setResponseBody(resUAStatus);
		
		return responseEntity;
	}
	
	/**
	 * 
	 * @Title: addDepositAccount 
	 * @Description: 根据用户id添加用户提现账户
	 * @param reqAddDepositAccount 添加用户提现账户请求Bean
	 * @author YHQ 
	 * @since V2.5.8
	 * @return ResponseEntity<Object>    返回类型
	 * 
	 */
	@Override
	public ResponseEntity<Object> addDepositAccount(ReqAddDepositAccount reqAddDepositAccount) {
		
		// 客户端响应Bean
		ResponseEntity<Object> response = new ResponseEntity<Object>();
		
		// 判断客户端请求参数是否为空
		if(reqAddDepositAccount == null || !ToolUtil.validateParam(reqAddDepositAccount)){
			
			response.setCode(HttpStatus.SC_BAD_REQUEST);
			response.setErrorMessage("客户端请求参数为空");
			response.setPromptMessage("请求参数异常");
			return response;
		}
		
		// 创建提现账户对象
		PassangerDepositAccount depositAccount = new PassangerDepositAccount();
		
		// 验证添加用户账户条件
		if(!this.validateAddAccountCondition(reqAddDepositAccount, depositAccount, response)){
			return response;
		}
		
		if(depositAccount.getDepositAccountId() == null){
			// 提现账户
			depositAccount.setDepositAccount(reqAddDepositAccount.getAlipayAccount());
			// 提现账户姓名
			depositAccount.setDepositAccountName(reqAddDepositAccount.getAlipayName());
			// 用户id
			depositAccount.setPassangerUserId(reqAddDepositAccount.getUserId());
			// 创建时间
			depositAccount.setCreateTime(new Date());
			// 提现账户状态   1:启用   2:禁用
			depositAccount.setDepositAccountStatus(1);
		}else{
			// 提现账户状态   1:启用   2:禁用
			depositAccount.setDepositAccountStatus(1);
		}
		
		// 根据用户id添加用户提现账户 true:乘客   false:失败
		boolean flag =  depositAccountDao.addDepositAccount(depositAccount);
		
		if(!flag){
			response.setCode(HttpStatus.SC_BAD_REQUEST);
			response.setErrorMessage("添加用户提现账户失败");
		}else{
			response.setCode(HttpStatus.SC_OK);
			response.setErrorMessage("添加用户提现账户成功");
		}
		
		return response;
	}

	/**
	 * 
	 * @Title: validateAddAccountCondition 
	 * @Description: 验证添加账户条件
	 * @param userId 用户Id
	 * @author YHQ 
	 * @since V2.6.3
	 * @return void    返回类型
	 * 
	 */
	public boolean validateAddAccountCondition(ReqAddDepositAccount reqAddDepositAccount,PassangerDepositAccount depositAccount,ResponseEntity<Object> response){
		
		// 获取该用户的全部账户列表(包括启用和禁用)
		List<PassangerDepositAccount> passangerDepositAccounts = depositAccountDao.getPassangerDepositAccount(reqAddDepositAccount.getUserId());
		
		// 如果该用户的用户列表不为空，则需要判断账户是否超过五个，已经新添加的用户账户是否存在
		if(passangerDepositAccounts != null && passangerDepositAccounts.size() > 0){
			
			// 启用账户个数
			int number = 0;
			// 是否存在相同账户 true:存在   false:不存在
			boolean flag = false;
			
			// 遍历全部用户账户列表
			for (PassangerDepositAccount passangerDepositAccount : passangerDepositAccounts){
				
				// 统计启用个数
				if(passangerDepositAccount.getDepositAccountStatus() == 1){
					number++;
				}
				
				// 判断用户列表中是否存在该提现账户
				if(passangerDepositAccount.getDepositAccount().equals(reqAddDepositAccount.getAlipayAccount())){
					if(passangerDepositAccount.getDepositAccountStatus() == 1){ // 启用状态
						flag = true;
					}else if(passangerDepositAccount.getDepositAccountStatus() == 2){ // 禁用状态
						depositAccount = passangerDepositAccount;
					}
				}
			}
			
			// 添加账户个数不能超过5个
			if(number > 4){
				response.setCode(HttpStatus.SC_BAD_REQUEST);
				response.setErrorMessage("您最多只能添加5个用户账户");
				response.setPromptMessage("您最多只能添加5个用户账户");
				return false;
			}
			
			// 该账户是否存在
			if(flag){
				response.setCode(HttpStatus.SC_BAD_REQUEST);
				response.setErrorMessage("该账户已存在");
				response.setPromptMessage("该账户已存在");
				return false;
			}
			
		}
		
		return true;
	}
	
	/**
	 * 
	 * @Title: deleteDepositAccount 
	 * @Description: 根据用户id删除提现账户
	 * @param @return    设定文件 
	 * @author YHQ 
	 * @since V2.5.8 
	 * @return ResponseEntity<Object>    返回类型
	 * 
	 */
	@Override
	public ResponseEntity<Object> deleteDepositAccount(ReqDeleteDepositAccount reqDeleteDepositAccount) {
		
		ResponseEntity<Object> responseEntity = new ResponseEntity<Object>();
		
		// 判断客户端请求参数是否为空
		if(reqDeleteDepositAccount == null || !ToolUtil.validateParam(reqDeleteDepositAccount)){
			
			responseEntity.setCode(HttpStatus.SC_BAD_REQUEST);
			responseEntity.setErrorMessage("客户端请求参数为空");
			responseEntity.setPromptMessage("请求参数为空");
			
			return responseEntity;
		}
		
		// 根据用户id删除提现账户  true:成功   false:失败
		boolean flag = depositAccountDao.deleteDepositAccount(reqDeleteDepositAccount.getDepositAccountId(),reqDeleteDepositAccount.getUserId());
		
		if(flag){
			responseEntity.setCode(HttpStatus.SC_OK);
			responseEntity.setErrorMessage("根据用户id删除提现账户成功");
			responseEntity.setPromptMessage("根据用户id删除提现账户成功");
		}else{
			responseEntity.setCode(HttpStatus.SC_BAD_REQUEST);
			responseEntity.setErrorMessage("根据用户id删除提现账户失败");
			responseEntity.setPromptMessage("根据用户id删除提现账户失败");
		}
		
		return responseEntity;
	}

	/**
	 * 
	 * @Title: getDepositAccountList 
	 * @Description: 根据用户id获取提现账户列表
	 * @param @return    设定文件 
	 * @author YHQ 
	 * @since V2.5.8
	 * @return ResponseEntity<Object>    返回类型
	 * 
	 */
	@Override
	public ResponseEntity<Object> getDepositAccountList(ReqGetDepositAccount reqGetDepositAccount) {
		
		ResponseEntity<Object> responseEntity = new ResponseEntity<Object>();
		
		GetTailwindDepositAccountDTO getTailwindDepositAccountDTO = new GetTailwindDepositAccountDTO();
		
		// 判断客户端请求参数是否为空
		if(reqGetDepositAccount == null || !ToolUtil.validateParam(reqGetDepositAccount)){
			
			responseEntity.setCode(HttpStatus.SC_BAD_REQUEST);
			responseEntity.setErrorMessage("客户端请求参数为空");
			responseEntity.setPromptMessage("请求参数异常");
			return responseEntity;
		}
		
		// 根据用户id获取提现账户列表
		List<ResGetDepositAccount> getDepositAccounts = depositAccountDao.getDepositAccountList(reqGetDepositAccount.getUserId());
		
		getTailwindDepositAccountDTO.setAccountList(getDepositAccounts);
		
		responseEntity.setCode(HttpStatus.SC_OK);
		responseEntity.setResponseBody(getTailwindDepositAccountDTO);
		
		return responseEntity;
	}

	/**
	 * 
	 * @Title: withdrawals 
	 * @Description: 用户提现(乘客端)
	 * @param reqWithdrawals 用户提现(乘客端)客户端请求Bean
	 * @author YHQ 
	 * @since V2.6.3
	 * @return ResponseEntity<Object>    返回类型
	 * 
	 */
	@Override
	public ResponseEntity<Object> withdrawals(ReqWithdrawals reqWithdrawals) {
		
		ResponseEntity<Object> responseEntity = new ResponseEntity<Object>();
		
		TailwindWithdrawalsDTO tailwindWithdrawalsDTO = new TailwindWithdrawalsDTO();
		
		// 判断客户端请求参数是否为空
		if(reqWithdrawals == null || !ToolUtil.validateParam(reqWithdrawals)){
			
			responseEntity.setCode(HttpStatus.SC_BAD_REQUEST);
			responseEntity.setErrorMessage("客户端请求参数为空");
			responseEntity.setPromptMessage("请求参数异常");
			
			return responseEntity;
		}
		
		// 获取用户登录信息
		UserLogin userLogin = tailwindUserInfoFacade.getUserLogin(reqWithdrawals.getUserId());
		
		// 获取用户账户信息
		Useraccount useraccount = tailwindUserAccountDao.getUserAccountById(reqWithdrawals.getUserId());
		
		// 用户是否存在
		if(userLogin == null || useraccount == null){
			responseEntity.setCode(HttpStatus.SC_BAD_REQUEST);
			responseEntity.setErrorMessage("该用户不存在");
			responseEntity.setPromptMessage("该用户不存在");
			
			return responseEntity;
		}
		
		// 是否设置提现密码   1:未提现  2:提现
		if(userLogin.getIsDeposit() != 2){
			
			responseEntity.setCode(HttpStatus.SC_BAD_REQUEST);
			responseEntity.setErrorMessage("该用户未设置提现密码");
			responseEntity.setPromptMessage("该用户未设置提现密码");
			
			return responseEntity;
		}
		
		// 本日是否已提现
		if(userLogin.getDepositNumber() > 0){
			responseEntity.setCode(HttpStatus.SC_BAD_REQUEST);
			responseEntity.setErrorMessage("本日提现次数已使用");
			responseEntity.setPromptMessage("本日提现次数已使用");
			return responseEntity;
		}
		
		// 判断提现密码是否输入正确
		if(!reqWithdrawals.getDepositPwd().equals(userLogin.getDepositPwd())){
			
			responseEntity.setCode(HttpStatus.SC_OK);
			tailwindWithdrawalsDTO.setDepositStatus(1);
			tailwindWithdrawalsDTO.setDepositInfo("提现密码错误");
			responseEntity.setResponseBody(tailwindWithdrawalsDTO);
			
			return responseEntity;
		}
		
		// 提现金额不允许小于1块钱
		if(reqWithdrawals.getDepositMoney().compareTo(new BigDecimal(1)) < 0){
			responseEntity.setCode(HttpStatus.SC_BAD_REQUEST);
			responseEntity.setErrorMessage("提现金额不允许小于1块钱");
			responseEntity.setPromptMessage("提现金额不允许小于1块钱");
			return responseEntity;
		}
		
		// 判断该用户账户余额是否足够提现
		if(useraccount.getCurrencyAmount().subtract(reqWithdrawals.getDepositMoney()).compareTo(new BigDecimal(0)) < 0){
			
			responseEntity.setCode(HttpStatus.SC_OK);
			tailwindWithdrawalsDTO.setDepositStatus(2);
			tailwindWithdrawalsDTO.setDepositInfo("余额不足");
			
			responseEntity.setResponseBody(tailwindWithdrawalsDTO);
			
			return responseEntity;
		}
		
		// 添加提现明细
		this.addDepositDetail(reqWithdrawals);
		// 修改提现后的账户余额
		this.updateUserAccount(reqWithdrawals, useraccount);
		// 修改用户本日提现次数
		this.updateUserLogin(userLogin);
		
		responseEntity.setCode(HttpStatus.SC_OK);
		tailwindWithdrawalsDTO.setDepositStatus(3);
		tailwindWithdrawalsDTO.setDepositInfo("提现成功");
		responseEntity.setResponseBody(tailwindWithdrawalsDTO);			
		
		return responseEntity;
	}

	
	/**
	 * 
	 * @Title: addDepositDetail 
	 * @Description: 添加提现明细
	 * @param reqWithdrawals 客户端请求参数 
	 * @author YHQ 
	 * @since V2.6.3
	 * @return boolean    返回类型
	 * 
	 */
	public boolean addDepositDetail(ReqWithdrawals reqWithdrawals){
		
		Depositdetail depositdetail = new Depositdetail();
		
		depositdetail.setDdId(ToolUtil.getUid());
		depositdetail.setDepositMoney(reqWithdrawals.getDepositMoney());
		depositdetail.setDepositStatus(1);
		depositdetail.setDepositTime(new Date());
		depositdetail.setUserId(reqWithdrawals.getUserId());
		depositdetail.setZfbAccount(reqWithdrawals.getZfbAccount());
		depositdetail.setZfbName(reqWithdrawals.getZfbName());
		
		return tailwindUserAccountDao.addDepositDetail(depositdetail);
	}
	
	/**
	 * 
	 * @Title: updateUserAccount 
	 * @Description: 修改用户账户信息
	 * @param reqWithdrawals 客户端请求参数
	 * @param useraccount 用户账户信息
	 * @author YHQ 
	 * @since V2.6.3
	 * @return boolean    返回类型
	 * 
	 */
	public boolean updateUserAccount(ReqWithdrawals reqWithdrawals,Useraccount useraccount){
		
		// 账户余额
		useraccount.setCurrencyAmount(useraccount.getCurrencyAmount().subtract(reqWithdrawals.getDepositMoney()));
		// 累计提现
		useraccount.setTotalDeposit(useraccount.getTotalDeposit().add(reqWithdrawals.getDepositMoney()));
		
		return tailwindUserAccountDao.updateUserAccount(useraccount);
	}
	
	/**
	 * 
	 * @Title: updateUserLogin 
	 * @Description: 修改本日提现次数
	 * @param userLogin 用户登录信息
	 * @author YHQ 
	 * @since V2.6.3
	 * @return boolean    返回类型
	 * 
	 */
	public boolean updateUserLogin(UserLogin userLogin){
		
		// 本日提现次数
		userLogin.setDepositNumber(userLogin.getDepositNumber() + 1);
		
		return tailwindUserInfoFacade.updateUserLogin(userLogin);
	}
	
}
